package org.yjw.service.user.impl;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yjw.dao.user.UserMapper;
import org.yjw.entity.user.User;
import org.yjw.service.user.UserService;

import javax.annotation.Resource;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    private static UserMapper userMapper;
    @Resource
    private SqlSession sqlSession;


    @Override
    public User findByid(Long id) {
        if (userMapper == null) {
            getUserMapper();
        }
        User byid = userMapper.findByid(id);
        return byid;
    }

    @Transactional
    @Override
    public Long insert(User user) {
        if (userMapper == null) {
            getUserMapper();
        }
        Long insert = userMapper.insert(user);
        System.out.println(1 / 0);
        return insert;
    }

    public void getUserMapper() {
        if (sqlSession == null) {
            return;
        }
        userMapper = sqlSession.getMapper(UserMapper.class);
    }
}
